<?php
namespace Admin\Controller;
use Think\Controller;
class MessageController extends BaseController {
	public function index() 
	{
		$id = I('get.id');
		$article = M('article')->where(
			array(
				'id' => $id
			)
		)->find();

		
		$message = M('message');
		$message_list = $message->where(
			array(
				'article_id'=> $id
				)
			)->select();
		
		$this->assign('article', $article);
		$this->assign('message_list', $message_list);
		$this->display();
		
	}

	public function add ()
	{
		$message_content = $_POST['content'];
		$article_id = I('post.id');
		$time = time();
		$data = array();
		
		$data['message_content'] = $message_content;
		$data['article_id'] = $article_id;
		$data['message_name'] = session('username');
		$data['message_time'] = $time;
		$message = M('message');
		

		$i = $message->add($data);
		if ($i===false) {
			$this->error('失败');
		}

	    $this->ajaxReturn($data, 'json');
		
	}

	public function sortsc ()
	{
		$id = 5;
		$message = M('message');
		$order = I('get.order');
		
			if($order===0) {
				
				$message_list = $message->where(
					array(
							'article_id'=> $id
					)
				)->order('hd_message.message_time asc')
				 ->select();
			 } else {
			 	
			 	$message_list = $message->where(
				array(
					'article_id'=> $id
					)
				)->order('hd_message.message_time desc')
				 ->select();
			 }
			$this->assign('message_list', $message_list);
			$this->display('index');


	}


}